package com.sctvcloud.pingwu.ui.adapter.holder;

import android.content.Context;
import android.content.Intent;
import android.text.TextUtils;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;

import com.bumptech.glide.load.engine.DiskCacheStrategy;
import com.ruihang.generalibrary.ui.widget.CustomFontTextView;
import com.sctvcloud.pingwu.R;
import com.sctvcloud.pingwu.beans.HomeFDBean;
import com.sctvcloud.pingwu.beans.NewsInfoAdvertBean;
import com.sctvcloud.pingwu.ui.activities.AdvertPicInfoActivity;
import com.sctvcloud.pingwu.ui.activities.H5NormalActivity;
import com.sctvcloud.pingwu.ui.utils.GlideUtil;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

public class HomeFDHolder extends BaseAbsHolder<HomeFDBean> implements View.OnClickListener {
    @BindView(R.id.small_fd_img)
    protected ImageView smallFdImg;
    @BindView(R.id.big_fd_img)
    protected ImageView bigFdImg;
    @BindView(R.id.ad_title)
    protected CustomFontTextView adTitle;
    @BindView(R.id.ad_resource)
    protected CustomFontTextView adResource;
    @BindView(R.id.ad_resource_layout)
    protected LinearLayout adResourceLayout;


    @BindView(R.id.top_divider)
    protected View topDivider;
    @BindView(R.id.bottom_divider)
    protected View bottomDivider;

    private HomeFDBean item;
    private OnHomeADListener homeADListener;

    public HomeFDHolder(Context context, View itemView, OnHomeADListener homeADListener) {
        super(context, itemView);
        this.context = context;
        itemView.setOnClickListener(this);
        this.homeADListener = homeADListener;
    }

    @Override
    protected void initInject() {
        ButterKnife.bind(this, itemView);
    }

    @Override
    public void setData(HomeFDBean t) {
        item = t;
        if (t.isBig()) {
            smallFdImg.setVisibility(View.GONE);
            bigFdImg.setVisibility(View.VISIBLE);
            GlideUtil.getGlid(context, t.getAdvertImage()).diskCacheStrategy(DiskCacheStrategy.NONE).skipMemoryCache(true).into(bigFdImg);
        } else {
            smallFdImg.setVisibility(View.VISIBLE);
            bigFdImg.setVisibility(View.GONE);
            GlideUtil.getGlid(context, t.getAdvertImage()).diskCacheStrategy(DiskCacheStrategy.NONE).skipMemoryCache(true).into(smallFdImg);
        }
        adTitle.setText(t.getAdvertTitle());
        adResource.setText(t.getResource());
        if (t.isShowTopDivider()) {
            topDivider.setVisibility(View.VISIBLE);
        } else {
            topDivider.setVisibility(View.GONE);
        }
        if (t.isShowBottomDivider()) {
            bottomDivider.setVisibility(View.VISIBLE);
        } else {
            bottomDivider.setVisibility(View.GONE);
        }
        if (null != homeADListener) {
            homeADListener.OnADLoad(item);
        }
        if ("0".equalsIgnoreCase(t.getIfDisplayAdvertisingWords())) {
            adResourceLayout.setVisibility(View.VISIBLE);
        } else {
            adResourceLayout.setVisibility(View.GONE);
        }
    }

    @Override
    public void onDestory() {
        super.onDestory();
        if (itemView != null) {
            itemView.setOnClickListener(null);
        }
    }

    @Override
    public void onClick(View v) {
        if (!TextUtils.isEmpty(item.getAdvertType())&&"0".equalsIgnoreCase(item.getAdvertType()) && !TextUtils.isEmpty(item.getContentImage())) {
            //跳转图片预览
            Intent intent = new Intent(context, AdvertPicInfoActivity.class);
            List<String> pics = new ArrayList<>();
            pics.add(item.getContentImage());
            intent.putExtra(AdvertPicInfoActivity.PIC_LIST, (Serializable) pics);
            List<NewsInfoAdvertBean> picInfo = new ArrayList<>();
            NewsInfoAdvertBean infoBean = new NewsInfoAdvertBean();
            infoBean.setId("" + item.getAdvertId());
            infoBean.setContentImage(item.getContentImage());
            infoBean.setH5(item.getAdvertH5());
            infoBean.setName(item.getAdvertTitle());
            picInfo.add(infoBean);
            intent.putExtra(AdvertPicInfoActivity.PIC_INFO_LIST, (Serializable) picInfo);
            context.startActivity(intent);
            if (null != homeADListener) {
                homeADListener.OnADLoadInfo(item);
            }
        } else if (!TextUtils.isEmpty(item.getAdvertType())&&"2".equalsIgnoreCase(item.getAdvertType()) && !TextUtils.isEmpty(item.getAdvertH5())) {
            //跳转h5外链
            Intent intent = new Intent(context, H5NormalActivity.class);
            intent.putExtra(H5NormalActivity.EX_HTML, item.getAdvertH5());
            intent.putExtra(H5NormalActivity.NEWS_ID, item.getAdvertId());
            intent.putExtra(H5NormalActivity.IS_ADVERT_H5, true);
            intent.putExtra(H5NormalActivity.IS_NEED_SHARE, true);
            intent.putExtra(H5NormalActivity.IS_NEED_TOKEN, false);
            context.startActivity(intent);
            if (null != homeADListener) {
                homeADListener.OnADLoadInfo(item);
            }
        }

    }

    public interface OnHomeADListener {
        void OnADLoad(HomeFDBean item);

        void OnADLoadInfo(HomeFDBean item);
    }
}
